package Leo.serviceProviders.config;

import com.netflix.appinfo.HealthCheckHandler;
import com.netflix.appinfo.InstanceInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.health.Status;
import org.springframework.stereotype.Component;

/**
 * 健康检查处理器。Eureka会自动启动一个定时器，默认30秒，定时刷新本地实例的信息，并把实例的状态更新到服务器。
 */
@Component
public class HealthCheckHandlerImpl implements HealthCheckHandler {

    @Autowired
    HealthIndicatorImpl healthIndicator;
    public InstanceInfo.InstanceStatus getStatus(InstanceInfo.InstanceStatus instanceStatus) {
        Status status = healthIndicator.health().getStatus();
        if(Status.UP.equals(status)){
            System.out.println("我是healthIndicator出发的状态处理器，目前状态正常");
            return InstanceInfo.InstanceStatus.UP;
        }else if(Status.DOWN.equals(status)){
            System.out.println("down机中");
            return InstanceInfo.InstanceStatus.DOWN;
        }else if(Status.OUT_OF_SERVICE.equals(status)){
            System.out.println("没有服务");
            return InstanceInfo.InstanceStatus.OUT_OF_SERVICE;
        }else if(Status.UNKNOWN.equals(status)){
            System.out.println("未知状态");
            return InstanceInfo.InstanceStatus.UNKNOWN;
        }else{
            return InstanceInfo.InstanceStatus.STARTING;
        }
    }
}
